﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml.Linq;

namespace bstest
{
    class dataIO
    {
        public struct ImpellerData
        {
            public double a, j, l;
        }
        
		//数据的初始化
        public static void InitializationData()
        {
            int calMode = 0, number = 0;
            double length = 0, elasticity = 0, density = 0, frequency = 0 ;
            string filename = "data.xml";
            ImpellerData[] data = new ImpellerData[number + 1];
            data[0].a = 0; data[0].j = 0; data[0].l = 0;
            WriteData(filename,calMode, data, number, length, elasticity, density);
            double[] shape = new double[51];
            WriteAns(number, shape, frequency, length);
        }

		//写入数据文件
        public static void WriteData(string filename,int calMode, ImpellerData[] data , int number, double length, double elasticity, double density)
        {
            XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
                                           new XElement("root",                           
                                                new XElement("calMode",calMode),
                                                new XElement("number", number),
                                                new XElement("length", length),
                                                new XElement("elasticity", elasticity),
                                                new XElement("density", density),
                                                new XElement("data")
                                           ));

            int i;
            for (i = 0; i <= number; i++)
            {
                XElement x = new XElement("data" + i.ToString());
                xdoc.Element("root").Element("data").Add(x);
                XElement xa = new XElement("a",data[i].a);
                XElement xj = new XElement("j", data[i].j);
                XElement xl = new XElement("l", data[i].l);
                xdoc.Element("root").Element("data").Element("data" + i.ToString()).Add(xa);
                xdoc.Element("root").Element("data").Element("data" + i.ToString()).Add(xj);
                xdoc.Element("root").Element("data").Element("data" + i.ToString()).Add(xl);
            }
            xdoc.Save(filename);
        }

		
		//读取数据文件
        public static void ReadData(string filename,out int calMode, out ImpellerData[] data, out int number, out double length, out double elasticity, out double density)
        {
            XDocument xdoc = XDocument.Load(filename);
            calMode = int.Parse(xdoc.Element("root").Element("calMode").Value);
            number = int.Parse(xdoc.Element("root").Element("number").Value);
            length = double.Parse(xdoc.Element("root").Element("length").Value);
            elasticity = double.Parse(xdoc.Element("root").Element("elasticity").Value);
            density = double.Parse(xdoc.Element("root").Element("density").Value);
            int i;
            data = new ImpellerData[51];
            for (i=0;i<= number; i++)
            {
                data[i].a = double.Parse(xdoc.Element("root").Element("data").Element("data" + i.ToString()).Element("a").Value);
                data[i].j = double.Parse(xdoc.Element("root").Element("data").Element("data" + i.ToString()).Element("j").Value);
                data[i].l = double.Parse(xdoc.Element("root").Element("data").Element("data" + i.ToString()).Element("l").Value);
            }
            data[number + 1].a = 0; data[number + 1].j = 0; data[number + 1].l = 0;
        }

		//写入有振型结果文件
        public static void WriteAns(int number, double[] shape, double frequency, double length)
        {
            XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
                                           new XElement("root",
                                                new XElement("frequency", frequency),
                                                new XElement("number",number),
                                                new XElement("shape")
                                           ));
            int i;
            for (i = 0; i <= number; i++)
            {
                XElement x = new XElement("shape" + i.ToString());
                xdoc.Element("root").Element("shape").Add(x);
                XElement xv = new XElement("value", shape[i]);
                XElement xl = new XElement("length", i*length/number);
                xdoc.Element("root").Element("shape").Element("shape" + i.ToString()).Add(xv);
                xdoc.Element("root").Element("shape").Element("shape" + i.ToString()).Add(xl);
            }
            xdoc.Save("ans.xml");
        }

		//读取有振型结果文件
        public static void ReadAns(out int number,out double frequency, out double[] shape,out double[] length)
        {
            XDocument xdoc = XDocument.Load("ans.xml");
            number = int.Parse(xdoc.Element("root").Element("number").Value);
            frequency = double.Parse(xdoc.Element("root").Element("frequency").Value);
            shape = new double[number + 1];
            length = new double[number + 1];
            int i;
            for (i = 0; i <= number; i++)
            {
                shape[i] = double.Parse(xdoc.Element("root").Element("shape").Element("shape"+i.ToString()).Element("value").Value);
                length[i] = double.Parse(xdoc.Element("root").Element("shape").Element("shape" + i.ToString()).Element("length").Value);
            }
        }

		//写入无振型结果文件
        public static void WriteAnsMat(int count,double[] ans)
        {
            XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
                                           new XElement("root",
                                           new XElement("AnsNumber", count),
                                           new XElement("frequency")
                                           ));
            int i;
            for (i=1;i< count; i++)
            {
                XElement x = new XElement("frequency" + i.ToString(), ans[i]);
                xdoc.Element("root").Element("frequency").Add(x);
            }
            xdoc.Save("ans.xml");
        }

		//读取无振型结果文件
        public static void ReadAnsMat(out int number,out double[] frequency)
        {
            XDocument xdoc = XDocument.Load("ans.xml");
            number = int.Parse(xdoc.Element("root").Element("AnsNumber").Value);
            frequency = new double[number];
            int i;
            for (i = 1; i < number; i++)
                frequency[i] = double.Parse(xdoc.Element("root").Element("frequency").Element("frequency"+i.ToString()).Value);
        }
    }
}
